package com.ygsoft.activiti.service.impl;

import com.ygsoft.activiti.model.MyActivity;
import com.ygsoft.activiti.model.MyHistoricProc;
import com.ygsoft.activiti.service.HistoryServiceInf;
import org.activiti.engine.HistoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Comment;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 历史进程实现类
 *
 * @author zhx
 * @create 2017-11-22 15:08
 **/
@Component
public class HistoryServiceImpl implements HistoryServiceInf {
    @Autowired
    private HistoryService historyService;

    @Autowired
    private TaskService taskService;

    @Override
    public List<MyHistoricProc> getHistoryByBussinessKey(String processId, int page, int pageSize) {
        /**
         * 初始化分页
         */

        if(page != 0){
            page = page -1;
        }

        if(pageSize ==0){
            pageSize = 20;
        }

        List<HistoricActivityInstance> historicActivityInstances = historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(processId)
                .orderByHistoricActivityInstanceStartTime()
                .asc()
                .listPage(page, pageSize);

        List<Comment> processInstanceComments = taskService.getProcessInstanceComments(processId);

        List<MyHistoricProc> tempList = new ArrayList<>();

        for(HistoricActivityInstance hai:historicActivityInstances){
            if(isAddToReturnList(hai)){
                MyHistoricProc myHistoricProc = new MyHistoricProc(hai);
                //任务中继续获取其他的信息
                if (hai.getActivityType().equals("userTask")){
                    HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery().taskId(hai.getTaskId()).singleResult();
                    for (Comment c: processInstanceComments){
                        //批注匹配上
                        if(c.getTaskId().equals(hai.getTaskId())){
                            myHistoricProc = myHistoricProc.setComment(historicTaskInstance,c);
                        }
                    }
                }
                tempList.add(myHistoricProc);
            }
        }
        return tempList;
    }

    @Override
    public int getHistoryByBussinessKeyTotal(String processId) {
        List<HistoricActivityInstance> historicActivityInstances = historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(processId)
                .finished()
                .list();

        int ret = 0;

        for(HistoricActivityInstance h:historicActivityInstances){
            if(isAddToReturnList(h)){
                ret++;
            }
        }

        return ret;
    }

    @Override
    public List<MyActivity> getActivityList(int page, int pageSize) {
        List<HistoricProcessInstance> historicProcessInstances = historyService.createHistoricProcessInstanceQuery().orderByProcessInstanceStartTime().desc().listPage(page, pageSize);

        List<MyActivity> returnList = new ArrayList<>();

        for (HistoricProcessInstance h:historicProcessInstances){
            MyActivity myActivity = new MyActivity(h);
            returnList.add(myActivity);
        }

        return returnList;
    }


    @Override
    public int getActivityListTotal() {
        long historicProcessInstances = historyService.createHistoricProcessInstanceQuery().count();
        return new Long(historicProcessInstances).intValue();
    }

    public boolean isAddToReturnList(HistoricActivityInstance h){
        if(!StringUtils.isEmpty(h.getActivityName()) || h.getActivityType().equals("startEvent") || h.getActivityType().equals("endEvent")){
            return true;
        }else{
            return false;
        }
    }
}
